package com.ymsoft.myerp.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ymsoft.myerp.common.DTO;
import com.ymsoft.myerp.entity.Menu;
import com.ymsoft.myerp.service.IMenuService;
import com.ymsoft.myerp.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import com.ymsoft.myerp.common.BaseController;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 菜单信息 前端控制器
 * </p>
 *
 * @author ymsoft
 * @since 2020-04-12
 */
@Controller
@RequestMapping("/myerp/menu")
public class MenuController extends BaseController {

    @Autowired
    private IMenuService iMenuService;

//    @GetMapping("/menu")
//    public Map<String, Object> menu() {
//        return iMenuService.menu();
//    }

    @RequestMapping("/menulist")
    @ResponseBody
    public DTO<?> list() {
        return DTO.retSuccess(iMenuService.list());
    }

    /**
     * @param menu
     * @return
     */
    @PostMapping("/update")
    @ResponseBody
    public DTO<?> update(@Valid Menu menu) {
        menu.setUpdateAt(LocalDateTime.now());
        boolean isOk = iMenuService.updateById(menu);
        if(isOk) {
            return DTO.retSuccess(menu);
        } else {
            return DTO.retFailed();
        }
    }

    @PostMapping("/add")
    @ResponseBody
    public DTO<?> add(@Valid Menu menu) {
        menu.setCreateAt(LocalDateTime.now());
        boolean isOk = iMenuService.save(menu);
        if(isOk){
            return DTO.retSuccess(menu);
        }else{
            return DTO.retFailed();
        }
    }

    //查询全部菜单
    @RequestMapping("/all")
    @ResponseBody
    public DTO<?> listall() {
        List menuLst= iMenuService.list();
        return DTO.retSuccess(menuLst);
    }

    //删除菜单、物理删除
    @PostMapping("/del")
    @ResponseBody
    public DTO<?> del(@Valid Menu menu) {
        boolean isOk = iMenuService.removeById(menu.getId());
        if(isOk) {
            return DTO.retSuccess(menu);
        } else {
            return DTO.retFailed();
        }
    }

    @RequestMapping(value = "/getone/{id}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public DTO<?> getOne(@PathVariable("id") String id) {
        Menu menu = iMenuService.getById(id);
        if(menu != null) {
            return DTO.retSuccess(menu);
        }
        return DTO.retFailed();
    }

    //查询全部菜单
    @RequestMapping(value = "/table", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public DTO<?> table(@RequestParam(value = "title", required = false) String title,
                        @RequestParam(value = "id", required = false) String id) {
        QueryWrapper<Menu> q1 = new QueryWrapper<>();
        q1.eq("pid", 0);//总父级-管理系统
        Menu parentMenu = iMenuService.getOne(q1);

        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        if(parentMenu != null) {
            queryWrapper.ne("pid", 0);
            queryWrapper.ne("pid", parentMenu.getId());
        }
        if(StringUtils.isNotBlank(title)) {
            queryWrapper.like("title", title);
        }
        if(StringUtils.isNotBlank(id)) {
            queryWrapper.like("id", id);
        }
        List menuList = iMenuService.list(queryWrapper);
        return DTO.retSuccess(menuList);
    }
}
